/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package questao02;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;

/**
 *
 * @author SDKW
 */
public class LoggerDatabase implements Logger {

    private Connection conexao;

    /**
     Para realizar a persistencia dessa conexão deve ter um banco de dados
     * server : javadb
     * bd     : log
     * user   : log
     * senha  : logjava
     * table de log
     **  CREATE TABLE log ( log VARCHAR(256));
     * @param log
     */
    @Override
    public void log(String log) {
        try {
            conexao = DriverManager.getConnection("jdbc:derby://localhost:1527/log", "log", "logjava");

            String sql = "insert into log (log) values (?)";
            PreparedStatement stmt;
            stmt = conexao.prepareStatement(sql);

            stmt.setString(1, "LOG:: " + log);
            stmt.execute();
            stmt.close();   
            conexao.close();
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(LoggerDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void close() {
        try {
            conexao.close();
        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(LoggerDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
